﻿var modalOptions = {
    closeClass: 'closeX',
    overlayCss: { 'background-color': '#efe7e1' },
    overlay : 85
}

; (function ($)
{
    var ConnectionManager = function(options)
    {
        this.managerDialog = options && options.managerDialog? options.managerDialog : '';
    }

    $.extend(ConnectionManager.prototype,
    {
        init : function()
        {
            
        }
    });

    var connectionManagerInstance;

    $.extend($,
    {
        initManager : function(options)
        {
            if(!connectionManagerInstance)
            {
                connectionManagerInstance = new ConnectionManager(options);
                connectionManagerInstance.init();
            }
        },

        dialogWait : function(mode)
        {
            if(mode)
            {
                $("#dialogTable").hide();
                $('#dialogSpinner').show();
            }
            else
            {
                $("#dialogTable").show();
                $('#dialogSpinner').hide();
            }
        },

        connectToServer : function()
        {
            $('#modalDialog').empty();
            $('#modalDialog').load(connectionManagerInstance.managerDialog,function()
            {
                $(this).modal(modalOptions);
                $('#btnCancel').click(function()
                {
                    $.modal.close();
                });

                $('#btnSend').click(function()
                {
                    $.dialogWait(true);
                    if (!$('#host').val()) 
                    {
                        alert('Please enter host');
                    }
                    else
                    {
                        var data = {
                            'host' : $('#host').val(),
                            'port' : $('#port').val(),
                            'user' : $('#user').val(),
                            'password' : $('#password').val()                           
                        };
                        $.post('/Home/ConnectToServer',
                        data,
                        function(res)
                        {
                             $('#serversContainer').append('<li><a href="javascript:void(0)">' + data.host + '</a></li>');
                             
                             for(i in res.Dbs)
                             {
                                $('#dbsContainer').append('<li><a href="javascript:void(0)">' + res.Dbs[i] + '</a></li>');
                             }
                             
                             $.modal.close();
                        });
                    }
                });
            });
        }
    });

    $.extend($.fn,
    {
        
    });

})(jQuery);